之前我們已經介紹資料庫(Database)、資料表(Table)、索引(Index)、檢視(View)等物件的創建(CREATE)指令, 在MySQL中還提供了很多資料庫物件的管理指令(Data Definition Statements)
我們把Data Definition Statements分成四類
alter_specification:
table_options
| ADD [COLUMN] col_name column_definition
[FIRST | AFTER col_name ]
| ADD [COLUMN] (col_name column_definition,...)
| ADD {INDEX|KEY} [index_name]
[index_type] (index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]] PRIMARY KEY
[index_type] (index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]]
UNIQUE [INDEX|KEY] [index_name]
[index_type] (index_col_name,...) [index_option] ...
| ADD FULLTEXT [INDEX|KEY] [index_name]
(index_col_name,...) [index_option] ...
| ADD SPATIAL [INDEX|KEY] [index_name]
(index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]]
FOREIGN KEY [index_name] (index_col_name,...)
reference_definition
| ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
| CHANGE [COLUMN] old_col_name new_col_name column_definition
[FIRST|AFTER col_name]
| MODIFY [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
| DROP [COLUMN] col_name
| DROP PRIMARY KEY
| DROP {INDEX|KEY} index_name
| DROP FOREIGN KEY fk_symbol
| DISABLE KEYS
| ENABLE KEYS
| RENAME [TO] new_tbl_name
| ORDER BY col_name [, col_name] ...
| CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
| [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name]
| DISCARD TABLESPACE
| IMPORT TABLESPACE
| partition_options
| ADD PARTITION (partition_definition)
| DROP PARTITION partition_names
| COALESCE PARTITION number
| REORGANIZE PARTITION [partition_names INTO (partition_definitions)]
| ANALYZE PARTITION partition_names
| CHECK PARTITION partition_names
| OPTIMIZE PARTITION partition_names
| REBUILD PARTITION partition_names
| REPAIR PARTITION partition_names
| REMOVE PARTITIONING
2.8. ALTER TABLESPACE
2.9. ALTER VIEW, 完整語法如下:
ALTER
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
各位有沒有發現ALTER類少了ALTER INDEX和ALTER TRIGGER兩個指令, MySQL建議Trigger應該要先刪掉再重建, 這在我們詳細討論Trigger時再談.
ALTER INDEX指令已經包含在ALTER TABLE中了, 但我建議也是先刪再重建, 這樣可獲得軟佳的效率
3. 刪除類
刪掉物件的指令, 刪前最好確定物件的相依關係, 例如View中引用的Table最好刪之前先確定View
3.1. DROP DATABASE
3.2. DROP EVENT
3.3. DROP FUNCTION
3.4. DROP INDEX
3.5. DROP LOGFILE GROUP
3.6. DROP PROCEDURE
3.7. DROP SERVER
3.8. DROP TABLE
3.9. DROP TABLESPACE
3.10. DROP TRIGGER
3.31. DROP VIEW
4. 更名類
只有Database和Table可變更名稱, 但更名前要檢查相關的各種物件
5.1. RENAME DATABASE
5.2. RENAME TABLE
我們今天大致瀏覽一下資料定義指令, 明天我們看看有那些資料維護指令(Data Manipulation Statements)